﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace BadHabit.Factory.Views {

    /// <summary>
    /// Interaction logic for ProjectPerformanceReport.xaml
    /// </summary>
    public partial class ProjectPerformanceReport : BadHabit.WPFTools.ShapedWindow {

        public ProjectPerformanceReport() {
            InitializeComponent();
        }

        /// <summary>
        /// Loads the report.
        /// </summary>
        public void LoadReport() {

            string flowDocumentXAML;

            int year = DateTime.Today.Year;
            if(yearComboBox.SelectedItem!=null)
                year = (int) yearComboBox.SelectedItem;

            string template = "default.xslt";
            if (reportTemplateComboBox.SelectedItem != null)
                template = reportTemplateComboBox.SelectedItem.ToString() + ".xslt";

            try {
                if (memberComboBox.SelectedItem == null) {
                    flowDocumentXAML = Client.Service.GenerateProjectPerformanceReportXAML(year, null, template);
                } else {
                    Data.TeamDataSet.MemberRow memberRow = Helper.GetRowFromRowView<Data.TeamDataSet.MemberRow>(memberComboBox.SelectedItem);
                    flowDocumentXAML = Client.Service.GenerateProjectPerformanceReportXAML(year, memberRow.MemberId, template);                                
                }
            } catch (Exception ex) {
                Views.Error.ShowError(ex, global::BadHabit.Factory.Windows.Properties.Resources.COULD_NOT_READ_REPORT);
                return;
            }

            System.IO.MemoryStream stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(flowDocumentXAML));

            FlowDocument document = (FlowDocument)XamlReader.Load(stream);
            flowDocumentViewer.Document = document;

        }

        private bool IsCombosLoaded = false;
        private void ShapedWindow_Loaded(object sender, RoutedEventArgs e) {
            FillReportTemplateCombo();
            FillYearCombo();
            IsCombosLoaded = true;
            LoadReport();
        }

        private void FillYearCombo() {
            yearComboBox.Items.Clear();
            for (int i = 0; i <= 9; i++) {
                yearComboBox.Items.Add(DateTime.Today.Year - i);                
            }
            yearComboBox.SelectedIndex = 0;
        }

        private void FillReportTemplateCombo() {
            string[] templates = null;
            try {
                 templates = Client.Service.RetriveProjectPerformanceReportTemplateList();
            } catch (Exception ex) {
                Views.Error.ShowError(ex, global::BadHabit.Factory.Windows.Properties.Resources.COULD_NOT_READ_REPORTLIST);
                return;
            }
            for (int i = 0; i < templates.Length; i++)
                templates[i] = templates[i].Replace(".xslt", "");
            reportTemplateComboBox.ItemsSource = templates;
            reportTemplateComboBox.SelectedIndex = 0;
        }

        private void exportButton_Click(object sender, RoutedEventArgs e) {
            System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
            saveDialog.FileName = global::BadHabit.Factory.Windows.Properties.Resources.Project_Performance_Report + ".xps";
            saveDialog.Filter = "XPS File|*.xps";

            System.Windows.Forms.DialogResult d = saveDialog.ShowDialog();
            if (d == System.Windows.Forms.DialogResult.Cancel)
                return;

            Client.FactoryMainWindow.IsWaiting = true;
            BadHabit.XPSTools.XPSHelper.FlowDocumentToXps(flowDocumentViewer.Document, saveDialog.FileName, null, BadHabit.XPSTools.XPSHelper.XPSWriteMode.Overwrite);
            Client.FactoryMainWindow.IsWaiting = false;
        }

        private void printButton_Click(object sender, RoutedEventArgs e) {
            flowDocumentViewer.Print();
        }

        private void memberComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!IsCombosLoaded)
                return;
            LoadReport();
        }

        private void yearComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!IsCombosLoaded)
                return;
            LoadReport();
        }

        private void reportTemplateComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!IsCombosLoaded)
                return;
            LoadReport();
        }

    }


     
}
